package com.clz.hyperit.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.clz.hyperit.dao.ArticleDao;
import com.clz.hyperit.dao.CollectionDao;
import com.clz.hyperit.dao.CommentDao;
import com.clz.hyperit.dao.FollowDao;
import com.clz.hyperit.dao.NoteDao;
import com.clz.hyperit.dao.ReportDao;
import com.clz.hyperit.dao.UserDetailDao;
import com.clz.hyperit.entity.Article;
import com.clz.hyperit.entity.Collection;
import com.clz.hyperit.entity.Comment;
import com.clz.hyperit.entity.Follow;
import com.clz.hyperit.entity.Note;
import com.clz.hyperit.entity.Report;
import com.clz.hyperit.entity.UserDetail;
import com.clz.hyperit.exception.ApplicationRunTimeException;
import com.clz.hyperit.service.BlogService;
import com.clz.hyperit.vo.GetComment;
import com.clz.hyperit.vo.PageInfo;
import com.clz.hyperit.vo.ReportVo;


@Service("blogService")
public class BlogServiceImpl implements BlogService {
	@Autowired
	private UserDetailDao userDetailDao;
	
	@Autowired
	private CollectionDao collectionDao;
	@Autowired
	private ArticleDao articleDao;
	@Autowired
	private NoteDao noteDao;
	@Autowired
	private FollowDao followDao;
	@Autowired
	private ReportDao reportDao;
	@Autowired
	private CommentDao commentDao;
	

	private static Logger logger = LoggerFactory
			.getLogger(BlogServiceImpl.class);

	public List<UserDetail> getBlogs(Integer rank, Integer field,PageInfo pageInfo) {
		logger.debug("getBlogs");
		List<UserDetail> list = null;
		if(rank==1&&field==1){
			list = userDetailDao.findAll(pageInfo);
		}
		else if(rank==1&&field==2){
			
		}
		else if(rank==1&&field==3){
			
		}
		else if(rank==1&&field==4){
		
		}
		else if(rank==1&&field==5){
			
		}
		else if(rank==1&&field==6){
			
		}
		else if(rank==1&&field==7){
			
		}
		else if(rank==1&&field==8){
			
		}
		else if(rank==1&&field==9){
			
		}
		return list;
	}

	public Integer saveCollect(Integer userdetail_id, Article article, Integer type) {
		//创建收藏实体
		Collection collection = new Collection();
		collection.setUserDetail_id(userdetail_id);
		collection.setGoal_id(article.getId());
		collection.setType(type);
		//获取文章，收藏数+1
		if(type==0){
			Article article_1=articleDao.get(article.getId());
			if(userdetail_id==article_1.getUserDetail_id()){
				throw new ApplicationRunTimeException("不能收藏自己写的文章");
			}
			article_1.setCollecting(article_1.getCollecting()+1);
			articleDao.updateWithNotNullProperties(article_1);
		}else{
			Note note=noteDao.get(article.getId());
			if(userdetail_id==note.getId()){
				throw new ApplicationRunTimeException("errow:不能收藏自己写的随笔");
			}
			note.setCollecting(note.getCollecting()+1);
			noteDao.updateWithNotNullProperties(note);
		}
		return collectionDao.save(collection);
	}

	public void deleteCollect(Integer userdetail_id, Integer id,
			Integer type) {
		//收藏-1
		if(type==0){
			Article article_1=articleDao.get(id);
			article_1.setCollecting(article_1.getCollecting()-1);
			articleDao.updateWithNotNullProperties(article_1);
		}else{
			Note note=noteDao.get(id);
			note.setCollecting(note.getCollecting()-1);
			noteDao.updateWithNotNullProperties(note);
		}
		//删除收藏关联表
		collectionDao.delete(userdetail_id,id,type);
	}

	public Integer saveFollow(Integer userdetail_id, Integer target_id) {
		UserDetail target = userDetailDao.get(target_id);
		target.setFans(target.getFans()+1);
		userDetailDao.updateWithNotNullProperties(target);
		Follow follow = new Follow();
		follow.setUserDetail_id(userdetail_id);
		follow.setBlog_id(target_id);
		Follow follow_1 = followDao.get_userid_targetid(userdetail_id,target_id);
		if(follow_1!=null){
			throw new ApplicationRunTimeException("不能重复关注");
		}
		return followDao.save(follow);
	}

	public void deleteFollow(Integer userdetail_id, Integer target_id) {
		UserDetail target = userDetailDao.get(target_id);
		target.setFans(target.getFans()-1);
		userDetailDao.updateWithNotNullProperties(target);
		followDao.deleteBy_userdetail_id_target_id(userdetail_id,target_id);
	}

	public int updateReward(Integer userdetail_id, Integer target_id) {
		int flag = 0;
		UserDetail ud = userDetailDao.get(userdetail_id);
		Article target_article = articleDao.get(target_id);
		UserDetail target_user = userDetailDao.get(target_article.getUserDetail_id());
		if(ud.getHb()>=1&&userdetail_id!=target_user.getId()){
			target_article.setReward(target_article.getReward()+1);
			ud.setHb(ud.getHb()-1);
			target_user.setHb(target_user.getHb()+1);
			articleDao.updateWithNotNullProperties(target_article);
			userDetailDao.updateWithNotNullProperties(target_user);
			userDetailDao.updateWithNotNullProperties(ud);	
			flag=0;
		}
		if(ud.getHb()<1){flag=1;}
		else if(userdetail_id==target_user.getId()) flag=2;
		return flag;
		
	}

	public void saveReport(Report report) {
		reportDao.save(report);
	}

	public Comment saveComment(Comment comment) {
		return commentDao.get(commentDao.save(comment));
	}

	public List<Comment> getCommentList(PageInfo pageInfo,Integer target_id,
			Integer type) {
			return commentDao.getBytarget_idAndType(pageInfo,target_id,type);
	}

	public UserDetail getCommentUserInfo(Integer userDetail_id) {
		return userDetailDao.get(userDetail_id);
	}

	public List<UserDetail> getFollowUserDetail(Integer userdetail_id) {
		List<Follow> follow_list = followDao.getByUserDetailId(userdetail_id);
		List<UserDetail> ud_list=new ArrayList<UserDetail>();
		for(Follow follow : follow_list){
			UserDetail ud = userDetailDao.get(follow.getBlog_id());
			ud_list.add(ud);
		}
		return ud_list;
	}

	public int deleteComment(Integer userdetail_id, Integer comment_id) {
		int flag =0;
		Comment comment = commentDao.get(comment_id);
		if(comment.getType()!=3&&comment.getUserDetail_id()!=userdetail_id){
			flag=0;
			return flag;
		}
		commentDao.deleteById(comment_id);
		return 1;
	}

	public List<ReportVo> getReport() {
		List<Report> report_list = reportDao.findAll();
		List<ReportVo> reportvo_list = new ArrayList<ReportVo>();
		for(Report report : report_list){
			ReportVo reportvo = new ReportVo();
			UserDetail ud = userDetailDao.get(report.getUserDetail_id());
			reportvo.setUserdetail_id(ud.getId());
			reportvo.setUser_name(ud.getName());
			reportvo.setReason(report.getReason());
			reportvo.setCreatetime(report.getCreatetime());
			if(report.getType()==0){
				Article article = articleDao.get(report.getGoal_id());
				reportvo.setTarget_user_id(article.getUserDetail_id());
				reportvo.setType(report.getType());
				reportvo.setTarget_id(article.getId());
				reportvo.setTarget_user_name(userDetailDao.get(article.getUserDetail_id()).getName());
			}
			else{
				Note note = noteDao.get(report.getGoal_id());
				reportvo.setTarget_user_id(note.getUserDetail_id());
				reportvo.setType(report.getType());
				reportvo.setTarget_id(note.getId());
				reportvo.setTarget_user_name(userDetailDao.get(note.getUserDetail_id()).getName());
			}
			reportvo_list.add(reportvo);
		}
		return reportvo_list;
	}

	public List getCollection(Integer userdetail_id,Integer type,PageInfo pageInfo) {
	
		List result = new ArrayList();
		if(type==0){
			List<Collection> collect_list = collectionDao.findByUserId(userdetail_id,pageInfo,type);
			for(Collection collect:collect_list){
				Article article = articleDao.get(collect.getGoal_id());
				article.setContent_md(null);
				article.setContent_html(null);
				result.add(article);
			}
		}
		if(type==1){
			List<Collection> collect_list = collectionDao.findByUserId(userdetail_id,pageInfo,type);
			for(Collection collect:collect_list){
				Note note = noteDao.get(collect.getGoal_id());
				note.setContent_md(null);
				note.setContent_html(null);
				result.add(note);
			}
		}
		return result;
	}

	public boolean isfollow(Integer userdetail_id, Integer targer_id) {
		Follow follow = followDao.get_userid_targetid(userdetail_id,targer_id);
		if(follow==null) 
			return false;
		else  
			return true;
	}

}
